package com.tuhu.transaction;

import com.tuhu.shardingdemo.entity.Course;
import com.tuhu.shardingdemo.mapper.CourseMapper;
import com.tuhu.shardingdemo.mapper.DictMapper;
import com.tuhu.shardingdemo.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author lijunwei
 * @date 2022/9/28
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ShardingJDBCTransactionDemo {
    @Resource
    private CourseMapper courseMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DictMapper dictMapper;


    /**
     * application-01.properties inline策略 ---分表
     *  @Transactional 分表(不分库)情况下起作用: 一张表插入失败, 则多张表都会失败
     */
    @Test
    @Transactional
    public void addcourse1() {
        for (int i = 0; i < 10; i++) {
            Course c = new Course();
            c.setCname("java");
            c.setUserId(1001L);
            c.setCstatus("1");
            courseMapper.insert(c);
            //insert into course values ....
        }
    }

    /**
     * application-02.properties inline策略 ---分表
     *  @Transactional 在分库分表情况下起作用:
     *        不同库的不不同表都会插入失败, 因为 shardingJdbc对@Transactional做了增强,
     *        这个jar包sharding-transaction-xa-core(进行增强)
     */
    @Test
    @Transactional
    public void addcourse2() {
        for (int i = 0; i < 10; i++) {
            Course c = new Course();
            c.setCname("java");
            c.setUserId(1001L);
            c.setCstatus("1");
            courseMapper.insert(c);
            //insert into course values ....
        }
    }
}
